package com.hanbang.maige.widget.dialog;

import android.app.ActionBar;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.hanbang.maige.R;
import com.hanbang.maige.mode.javabean.login.BiaoQianData;
import com.hanbang.maige.utils.other.CountdownUtils;
import com.hanbang.maige.utils.other.EditHelper;
import com.hanbang.maige.utils.other.Validators;
import com.hanbang.maige.utils.ui.ScreenInfoUtils;
import com.hanbang.maige.view.login.activity.BiaoQianActivity;
import com.hanbang.maige.view.login.activity.LoginActivity;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;


/**
 * 作者　　: 杨阳
 * 创建时间: 2016/9/13 13:33
 * 邮箱　　：360621904@qq.com
 * <p>
 * 功能介绍：评论的输入
 */

public class DialogZhuChe extends Dialog {

    public EditHelper editHelper;
    @BindView(R.id.phoneTil)
    TextInputLayout phoneTil;
    @BindView(R.id.codeTil)
    TextInputLayout codeTil;
    @BindView(R.id.fasong)
    TextView fasong;
    @BindView(R.id.yaoqingmaTil)
    TextInputLayout yaoqingmaTil;
    @BindView(R.id.biaoqian)
    TextView biaoqian;
    private BiaoQianData.ChildLeiBean biaoQianData;
    LoginActivity activity;

    public DialogZhuChe(LoginActivity context) {
        super(context, R.style.Dialog_Botton_Form_top);
        this.activity = context;
        editHelper = new EditHelper(context);
        setContentView(R.layout.dialog_zhuche);
        ButterKnife.bind(this, this);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        WindowManager.LayoutParams wlp = getWindow().getAttributes();
        ScreenInfoUtils screen = new ScreenInfoUtils();
        wlp.width = screen.getWidth();
        wlp.height = ActionBar.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(wlp);
        getWindow().getAttributes().gravity = Gravity.BOTTOM;
        getWindow().setBackgroundDrawableResource(R.color.white);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
        setOnShowListener(new OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                Observable.timer(100, TimeUnit.MILLISECONDS)
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Action1<Long>() {
                            @Override
                            public void call(Long aLong) {
                                //UiUtils.showInput(textInputLayout);
                            }
                        });
            }
        });

        editHelper.addEditHelperData(new EditHelper.EditHelperData(
                phoneTil, Validators.getLengthSRegex(1, 11), "请正确输入11位手机号"
        ));
        editHelper.addEditHelperData(new EditHelper.EditHelperData(
                codeTil, Validators.getLengthSRegex(1, 4), "验证码为4位"
        ));
        editHelper.addEditHelperData(new EditHelper.EditHelperData(
                biaoqian, Validators.getLenghMinRegex(1), "请选择标签"
        ));

    }

    @Override
    public void show() {
        super.show();
    }

    @Override
    public void dismiss() {
        //   UiUtils.exitInput(textInputLayout);
        super.dismiss();
    }


    @OnClick({R.id.fasong, R.id.biaoqianRl, R.id.zhuche, R.id.dismiss})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fasong:
                if (editHelper.getEditHelperData(R.id.phoneTil).check(activity)) {
                    activity.presenter.getZhuCheCode(editHelper.getText(R.id.phoneTil));
                }
                break;
            case R.id.biaoqianRl:
                BiaoQianActivity.startUI(activity);

                break;
            case R.id.zhuche:
                if (editHelper.check()) {
                    activity.presenter.postZhuChe(
                            editHelper.getText(R.id.phoneTil),
                            editHelper.getText(R.id.codeTil),
                            biaoQianData.getID());
                }

                break;
            case R.id.dismiss:
                dismiss();
                break;
        }
    }

    public void setBiaoQian(BiaoQianData.ChildLeiBean data) {
        if (data != null) {
            biaoQianData = data;
            biaoqian.setText(data.getHyValueName());
        }
    }

    public void setCode() {
        CountdownUtils.recordTime(fasong);
    }
}
